/*  
 *  ------ [802_01] - configure XBee basic parameters -------- 
 *  
 *  Explanation: This program shows how to configure basic XBee
 *  parameters in order to communicate between different XBee 
 *  devices using the same network.   
 */

#include <MakeSureXBee802.h>

// PAN (Personal Area Network) Identifier
uint8_t  PANID[2]={0x33,0x32}; 

// 16-byte Encryption Key
char* KEY = "MakeSuremoteLinkKey!"; 

void setup()
{
  Serial.begin(9600);
  Serial.println("Serial Open");
  // init XBee 
  // It starts serial port - Baud rate - 9600
  xbee802.ON();
  Serial.println("XBEE ON");
    // wait for a second
  delay(100);

  /////////////////////////////////////
  // 1. set channel 
  /////////////////////////////////////
  xbee802.setChannel(0x0C);
  //When XBee is in API -2 mode then you need to send packet to configure XBee and when in transperent mode need to send AT command directly.
  //Printing buffer frame 
  //Serial.println(buffer);
  Serial.println("Setting Channel.. ");
  // check at commmand execution flag
  if( xbee802.error_AT == 0 ) 
  {
    Serial.println("Channel set OK");
  }
  else 
  {
    Serial.println("Channel  Set failed");
  }


  /////////////////////////////////////
  // 2. set PANID
  /////////////////////////////////////
  xbee802.setPAN(PANID);

  // check the AT commmand execution flag
  if( xbee802.error_AT == 0 ) 
  {
    Serial.println("PAN Set");
  }
  else 
  {
   Serial.println("Channel  Set error"); 
  }

  /////////////////////////////////////
  // 3. set encryption mode (1:enable; 0:disable)
  /////////////////////////////////////
  xbee802.setEncryptionMode(0);
  
  // check the AT commmand execution flag
  if( xbee802.error_AT == 0 ) 
  {
    Serial.println("encryption set OK");
  }
  else 
  {
    Serial.println("Error setting security");  
  }

  /////////////////////////////////////
  // 4. set encryption key
  /////////////////////////////////////
  xbee802.setLinkKey(KEY);

  // check the AT commmand execution flag
  if( xbee802.error_AT == 0 ) 
  {
    Serial.println("encryption key set OK");
  }
  else 
  {
    Serial.println("Error setting encryption key");  
  }

  /////////////////////////////////////
  // 5. write values to XBee module memory
  /////////////////////////////////////
  xbee802.writeValues();

  // check the AT commmand execution flag
  if( xbee802.error_AT == 0 ) 
  {
    Serial.println("write values OK");
  }
  else 
  {
    Serial.println("Error writing values");  
  }

  Serial.println("-------------------------------"); 
}

void loop()
{

  /////////////////////////////////////
  // 1. get channel 
  /////////////////////////////////////
  xbee802.getChannel();
   Serial.println("channel: ");
  Serial.println(xbee802.channel,HEX);
  /////////////////////////////////////
  // 2. get PANID
  /////////////////////////////////////
  xbee802.getPAN();
  Serial.println("panid: ");
  Serial.println(xbee802.PAN_ID[0], HEX); 
  Serial.println(xbee802.PAN_ID[1], HEX); 

  /////////////////////////////////////
  // 3. get encryption mode (1:enable; 0:disable)
  /////////////////////////////////////
  xbee802.getEncryptionMode();
  Serial.println("encryption mode: ");
  Serial.println(xbee802.encryptMode, HEX);

   Serial.println("-------------------------------"); 

  delay(3000);
}




